package com.zl.task.controller;


import com.zl.task.dto.ProductDTO;
import com.zl.task.service.IServiceProductService;
import com.zl.task.util.resp.RespResult;
import com.zl.task.util.resp.RespUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinlingeng
 * @since 2021-07-23
 */
@RestController
public class ServiceProductController {

    @Autowired
    private IServiceProductService productService;


    @ApiOperation("产品发布")
    @PostMapping("/saveProduct")
    public RespResult<String> saveProduct(ProductDTO productDTO) {
        try {
            return RespUtil.success(productService.saveProduct(productDTO));
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.error(e.getMessage());
        }
    }


    @ApiOperation("产品修改")
    @PostMapping("/updateProduct")
    public RespResult<Boolean> updateProduct(ProductDTO productDTO) {
        try {
            return RespUtil.success(productService.updateProduct(productDTO));
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.error(e.getMessage());
        }
    }

    @ApiOperation("产品上下架")
    @PostMapping("/hideProduct")
    public RespResult<Boolean> hideProduct(Long productId,Long userId,Integer productStatus) {
        try {
            return RespUtil.success(productService.hideProduct(productId,userId,productStatus));
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.error(e.getMessage());
        }
    }


    @ApiOperation("产品展示")
    @PostMapping("/listProduct")
    public RespResult<Object> listProduct(Integer typeId,Integer page,Integer size,String userType,Long userId) {
        try {
            return RespUtil.success(productService.listProduct(typeId,page,size,userType,userId));
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.error(e.getMessage());
        }
    }

    @ApiOperation("产品删除")
    @PostMapping("/deleteProduct")
    public RespResult<Object> deleteProduct(Long userId,String productId) {
        try {
            return RespUtil.success(productService.deleteProduct(userId,productId));
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.error(e.getMessage());
        }
    }


}

